#include "simulation.h"

int main(int argc, char * argv[])
{
	if (argc != 2)
	{
		cout << "Usage: " << argv[0] << " settings_filename" << endl;
		return 0;
	}

	// Open file with settings
	ifstream settings(argv[1], ifstream::in);
	if(!settings.is_open())
	{
		cout << "Error: can't open file '" << argv[1] << "'." << endl;
		return 1;
	}

	// Parse file and look for simulation definitions
	string line;
	cout << ";AVG WAIT;DENIED;AVG WAIT;DENIED;AVG WAIT;DENIED" << endl;
	cout << ";NORMAL;NORMAL;EXTEND;EXTEND;FUZZY;FUZZY" << endl;
	while (settings.good())
	{
		getline(settings, line);
		// Skip comments and empty lines
		if (line.empty() || line[0] == '#')
			continue;
		string paramname;
		stringstream linestr;
		linestr.str(line);
		linestr >> paramname;
		if (paramname == "Simulation")
		{
			// Simulation definition started
			// Save simulation name
			string simulation_name;
			getline(linestr,simulation_name);
			// Save all simulation settings from file into sim_settings
			string sim_settings;
			while (settings.good())
			{
				getline(settings,line);
				stringstream linestr2;
				linestr2.str(line);
				linestr2 >> paramname;
				if (paramname == "Simulation_end")
					break;
				// Append line to simulation settings stream
				sim_settings += line + "\n";
			}
			
			cout << simulation_name << ";";
			
			// Do simulation for all controllers
			for (int ctrl_num = 1; ctrl_num <= 3; ctrl_num++)
			{
				Simulation sim(simulation_name.substr(1), ctrl_num);
				stringstream ss_settings;
				ss_settings.str(sim_settings);
				if (sim.initialize(ss_settings) > 0)
					return 1;
				sim.doSimulation();
				//sim.printResults();
				cout << sim.getTotalAverage() << ";";
				cout << sim.getTotalDenied() << ";";
			}
			cout << endl;
		}
		else
		{
			cout << "Error while loading settings: unknown setting: " << paramname << endl;
			return 1;
		}
	}

	//system("pause");
	return 0;
}
